<?php

class Reale_Model_DbTable_AgentEarnings extends Zend_Db_Table_Abstract
{
    /**
     * @var string Name of the database table
     */
    protected $_name = 'agent_earnings';
    
    /**
     * 
     * @param mixed $dispatched "1 => dispatched, NULL => pending, ALL => ALL"
     * @param int $agent_id
     * @param string $trans_type BUSINESS/NETWORK/ACHIEVEMENT
     * @return array
     */
    protected function _earnings($agent_id = NULL,$dispatched = NULL, $dispatchDate = NULL, $transDate = NULL, $trans_type = NULL) {
        $select = $this->getAdapter()
                    ->select()
                    ->from('agent_earnings', 
                                array('ab_trans',
                                        'amount',
                                        'trans_date'))
                    ->join('agent',
                    		'`agent_earnings`.`agent_id` = `agent`.`agent_id`',
                            array('agent_reg_id','agent_name'));
        
        if ($agent_id) {
            $select->where('agent_earnings.agent_id = ?',$agent_id);
        } else {
            $select->columns('agent_earnings.agent_id');
        }
        
        if (!$dispatched) {
            $select->where('is_dispatched = 0');
        } elseif (1 === $dispatched) {
            $select->where('is_dispatched = 1');
        } else {
            $select->columns('is_dispatched');
        }
        
        if ($dispatchDate) {
            if (is_array($dispatchDate)) {
                $from = $dispatchDate['from'];
                $upto = $dispatchDate['upto'];
                $select->where("dispatch_date between $from and $upto");
            } else {
                $select->where('dispatch_date =?', $dispatchDate);
            }
        } else {
            $select->columns('dispatch_date');
        }
           
        if ($transDate) {
            $select->columns('trans_date');
        }
        
        if ($trans_type) {
            $select->where('agent_earnings.trans_type = ?',$trans_type);
        } else {
            $select->columns('agent_earnings.trans_type');
        }
        
        return $select->query()->fetchAll();
    }
    
    public function businessTransactions($agent_id = NULL,$dispatched = NULL, $dispatchDate = NULL, $transDate = NULL) {
        return self::_earnings($agent_id,$dispatched, $dispatchDate, $transDate, 'BUSINESS');
    }

    public function networkTransactions($agent_id = NULL,$dispatched = NULL, $dispatchDate = NULL, $transDate = NULL) {
        return self::_earnings($agent_id,$dispatched, $dispatchDate, $transDate, 'NETWORK');
    }
    

    /*
     * Alternative of these functions is create root agent and call it.
    public function rootNetworkTransactions($dispatched = NULL) {
        $agent_id = Reale_Model_Users_Agent::ROOT_AGENT_ID;
        return self::_earnings($dispatched, $agent_id, 'NETWORK');
    }
    
    public function rootBusinessTransactions($dispatched = NULL) {
        $agent_id = Reale_Model_Users_Agent::ROOT_AGENT_ID;
        return self::_earnings($dispatched, $agent_id, 'BUSINESS');
    }*/
}
